<?php
namespace app\dboard\controller;

use think\Db;
use think\Image;
use services\OssService;

class Upload extends Base
{
        protected $pic_extension = ['jpg', 'jpeg', 'png', 'gif'];
        protected $file_extension = ['doc', 'docx', 'pdf', 'xls', 'xlsx', 'ppt', 'pptx'];
        protected $video_extension = ['mp4'];
        protected $path;
        protected $domain;
        protected $folder = [];
        protected $max_size = 5;     //单位M
        protected $thumb_width = 200;
        protected $thumb_height = 200;

        function __construct() {
            $this->path = config('upload_path');
            $this->domain = config('upload_domain');
            $this->http_domain = config('http_domain');
            $this->folder = [''];
        }

        function _initialize() {
            parent::_initialize();
        }

        /**
         * 上传图片到oss
         */
        public function cover_upload_bak()
        {
            $upload_data = array();
            $upload_data['createtime'] = time();
            // $path = $this->path;
            $pdir = 'zsy/';
            if($_FILES['file']['error']){
                    return jsonReturn(array('ok'=>0,'msg'=>'上传出错'));
            }
            $extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
            if(!in_array($extension,$this->pic_extension) && !in_array($extension,$this->file_extension) && !in_array($extension,$this->video_extension)){
                    return jsonReturn(array('ok'=>0,'msg'=>'上传格式不允许'.$_FILES['file']['name']));
            }
            if(($_FILES['file']['size']/(50*1024*1024)) >= $this->max_size){
                    return jsonReturn(array('ok'=>0,'msg'=>'上传的文件超过限制'));
            }
            $file = file_get_contents($_FILES["file"]["tmp_name"]);
            $ossService = new OssService();
            // $face_content = get_img_content($face_img);
            /*@todo img*/
            $filename = 'zsy_info_'.date('YmdHi').rand_str(4,'0123456789').'.'.$extension;
            $filepath = $pdir.$filename;
            $upload_file=$ossService->upload_file_content($file,$pdir.$filename);
            //var_dump($upload_file);exit;
            return jsonReturn(array('ok'=>1,'msg'=>'上传成功','fileurl'=>$this->domain.$filepath,'insert_id'=>'/'.$filepath));
        }
        /**
         * 老师头像上传
         */
        public function cover_upload()
        {
            // $path = $this->path;
            if($_FILES['file']['error']){
                    return jsonReturn(array('ok'=>0,'msg'=>'上传出错'));
            }
            $extension = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION));
            if(!in_array($extension,$this->pic_extension) && !in_array($extension,$this->file_extension) && !in_array($extension,$this->video_extension)){
                    return jsonReturn(array('ok'=>0,'msg'=>'上传格式不允许'.$_FILES['file']['name']));
            }
            if(($_FILES['file']['size']/(50*1024*1024)) >= $this->max_size){
                    return jsonReturn(array('ok'=>0,'msg'=>'上传的文件超过限制'));
            }
            $pdir = 'cljy/';
            $file = file_get_contents($_FILES["file"]["tmp_name"]);
            $ossService = new OssService();
            $filename = 'cljy_info_'.date('YmdHi').rand_str(4,'0123456789').'.'.$extension;
            $filepath = $pdir.$filename;
            //文件名称
            $upload_data['name'] = $_FILES['file']['name'];
            //文件大小
            $upload_data['size'] = $_FILES['file']['size'];
            //文件路径
            $upload_data['path'] = $filepath;
            //mime类型
            $upload_data['extension'] = $_FILES['file']['type'];
            $upload_data['create_time'] = time();
            $insert_id = Db::name('file')->insertGetId($upload_data);
            if(!$insert_id){
                    return jsonReturn(array('ok'=>0,'msg'=>'上传出错'));
            }
            $upload_file=$ossService->upload_file_content($file,$pdir.$filename);
            $fileurl = $this->domain.$filepath;
            switch ($extension) {
                case 'pdf':
                    $fileurl = '/web/img/pdf.png';
                    break;
                case 'doc':
                    $fileurl = '/web/img/doc.png';
                    break;
                case 'docx':
                    $fileurl = '/web/img/doc.png';
                    break;
                case 'xls':
                    $fileurl = '/web/img/xls.png';
                    break;
                case 'xlsx':
                    $fileurl = '/web/img/xls.png';
                    break;
                case 'ppt':
                    $fileurl = '/web/img/ppt.png';
                    break;
                case 'pptx':
                    $fileurl = '/web/img/ppt.png';
                    break;
                case 'mp3':
                    $fileurl = '/web/img/music.png';
                    break;
                case 'swf':
                    $fileurl = '/web/img/music.png';
                    break;
                case 'mp4':
                    $fileurl = '/web/img/video.png';
                    break;
                case 'avi':
                    $fileurl = '/web/img/video.png';
                    break;
            }
            return jsonReturn(array('ok'=>1,'msg'=>'上传成功','fileurl'=>$fileurl,'insert_id'=>$filepath));
        }
        /**
         * 老师头像上传
         */
        public function avatar_upload()
        {
            // $path = $this->path;
            if($_FILES['file']['error']){
                    return jsonReturn(array('ok'=>0,'msg'=>'上传出错'));
            }
            $extension = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION));
            if(!in_array($extension,$this->pic_extension) && !in_array($extension,$this->file_extension) && !in_array($extension,$this->video_extension)){
                    return jsonReturn(array('ok'=>0,'msg'=>'上传格式不允许'.$_FILES['file']['name']));
            }
            if(($_FILES['file']['size']/(50*1024*1024)) >= $this->max_size){
                    return jsonReturn(array('ok'=>0,'msg'=>'上传的文件超过限制'));
            }
            $pdir = 'cljy/';
            $file = file_get_contents($_FILES["file"]["tmp_name"]);
            $ossService = new OssService();
            $filename = 'teacher_avatar_'.date('YmdHi').rand_str(4,'0123456789').'.'.$extension;
            $filepath = $pdir.$filename;
            //文件名称
            $upload_data['name'] = $_FILES['file']['name'];
            //文件大小
            $upload_data['size'] = $_FILES['file']['size'];
            //文件路径
            $upload_data['path'] = $filepath;
            //mime类型
            $upload_data['extension'] = $_FILES['file']['type'];
            $upload_data['create_time'] = time();
            $insert_id = Db::name('file')->insertGetId($upload_data);
            if(!$insert_id){
                    return jsonReturn(array('ok'=>0,'msg'=>'上传出错'));
            }
            $upload_file=$ossService->upload_file_content($file,$pdir.$filename);
            $fileurl = $this->domain.$filepath;
            switch ($extension) {
                case 'pdf':
                    $fileurl = '/web/img/pdf.png';
                    break;
                case 'doc':
                    $fileurl = '/web/img/doc.png';
                    break;
                case 'docx':
                    $fileurl = '/web/img/doc.png';
                    break;
                case 'xls':
                    $fileurl = '/web/img/xls.png';
                    break;
                case 'xlsx':
                    $fileurl = '/web/img/xls.png';
                    break;
                case 'ppt':
                    $fileurl = '/web/img/ppt.png';
                    break;
                case 'pptx':
                    $fileurl = '/web/img/ppt.png';
                    break;
                case 'mp3':
                    $fileurl = '/web/img/music.png';
                    break;
                case 'swf':
                    $fileurl = '/web/img/music.png';
                    break;
                case 'mp4':
                    $fileurl = '/web/img/video.png';
                    break;
                case 'avi':
                    $fileurl = '/web/img/video.png';
                    break;
            }
            return jsonReturn(array('ok'=>1,'msg'=>'上传成功','fileurl'=>$fileurl,'insert_id'=>$filepath));
        }


        /**
         * 用户头像上传文件入库
         * @return ycg_upload_photo.id
         */
        public function head_img_upload(){
                $upload_data = array();
                $upload_data['createtime'] = time();
                // $path = $this->path;
                $pdir = '/upload/cover/';
                if($_FILES['head_img']['error']){
                        return jsonReturn(array('ok'=>0,'msg'=>'上传出错'));
                }
                $extension = pathinfo($_FILES['head_img']['name'], PATHINFO_EXTENSION);
                if(!in_array($extension,$this->pic_extension) && !in_array($extension,$this->file_extension) && !in_array($extension,$this->video_extension)){
                        return jsonReturn(array('ok'=>0,'msg'=>'上传格式不允许'.$_FILES['head_img']['name']));
                }
                if(($_FILES['head_img']['size']/(1024*1024)) >= $this->max_size){
                        return jsonReturn(array('ok'=>0,'msg'=>'上传的文件超过限制'));
                }

                $path = $this->path.$pdir;
                if(!is_dir($path))
                        mkdir($path);
                $dirpath = $path.date('Ymd');
                if(!is_dir($dirpath))
                        mkdir($dirpath);

                //文件名称
                $upload_data['name'] = date('YmdHi').rand_str(6).'.'.$extension;
                //文件大小
                $upload_data['size'] = $_FILES['head_img']['size'];
                //文件路径
                $upload_data['path'] = $dirpath.'/' . $upload_data['name'];
                //mime类型
                $upload_data['type'] = $_FILES['head_img']['type'];

                if(!move_uploaded_file($_FILES["head_img"]["tmp_name"],$upload_data['path'])){
                        return jsonReturn(array('ok'=>0,'msg'=>'上传出错'));
                }
                //缩略图路径
                if(in_array($extension,$this->pic_extension)){
                        $upload_data['thumbpath'] = $this->compress($upload_data['path'],$pdir,$upload_data['name']);
                }

                //去掉路径前的点
                $upload_data['path'] = substr($upload_data['path'],mb_strlen($this->path));
                $upload_data['thumbpath'] = substr($upload_data['thumbpath'],mb_strlen($this->path));
                $insert_id = Db::name('UploadPhoto')->insertGetId($upload_data);
                if(!$insert_id){
                        return jsonReturn(array('ok'=>0,'msg'=>'上传出错'));
                }
                
                //用户头像id插入数据库
                $AdminUsers = Db::name('AdminUsers');
                $id = intval($_SESSION['adminInfo']['id']);
                $update_id = $AdminUsers->where(array('id'=>$id))->save(array('img'=>$insert_id));
                if($update_id){
                    return jsonReturn(array('ok'=>1,'msg'=>'上传成功','fileurl'=>$this->domain.$upload_data['path'],'insert_id'=>$insert_id));
                }else{
                    return jsonReturn(array('ok'=>0));
                }
        }

        /**
         * 图片压缩
         * @param $pic 图片完整路径
         * @param $pdir upload子目录
         * @param $name 图片名称
         * @return 压缩图片的地址
         */
        public function compress($pic,$pdir,$name)
        {
            if(empty($pic) || empty($pdir) || empty($name))
                return false;

            $image = Image::open($pic);
            
            
            //缩略图路径
            $path_array = explode($pdir, $pic);

            //缩略图文件夹
            $path_thumb = $this->path.$pdir.'thumb/';
            if(!is_dir($path_thumb))
                mkdir($path_thumb);

            //在缩略图文件夹建立时间区分的子文件夹
            $path_dir = $path_thumb.date('Ymd');
            if(!is_dir($path_dir))
                mkdir($path_dir);

            //缩略图的名称
            $name_array = explode('.',$name);
            $name = $name_array[0].'_thumb.'.$name_array[1];

            //缩略图路径
            $thumb_path = $path_dir.'/'.$name;
            //生成缩略图
            $res = $image->thumb($this->thumb_width, $this->thumb_height)->save($thumb_path);
            return $thumb_path;
        }

        /**
         * 图片水印
         * @return 水印图片路径
         */
        public function watermark()
        {

        }

        /**
         * 后台图片显示图片
         * 图片的html
         */
        public function show_photo() {
            $photo = input('cover','');
            $photo_id = input('cover_id','');
            $type = input('type','0');
            $photo = trim($photo,',');
            $photo_array = explode(',',$photo);
            $photo_id_array = explode(',',$photo_id);
            $html = "";
            foreach ($photo_array as $key => $value) {
                $photo_url = $value;
                if(substr($photo_url, -3)=='mp4') {
                    $photo_url = get_newpic_url($value).'?x-oss-process=video/snapshot,t_10000,m_fast';
                } else {
                    $photo_url = get_newpic_url($value);
                    $hz = substr($photo_url, -3);
                    switch ($hz) {
                        case 'pdf':
                            $photo_url = '/web/img/pdf.png';
                            break;
                        case 'doc':
                            $photo_url = '/web/img/doc.png';
                            break;
                        case 'ocx':
                            $photo_url = '/web/img/doc.png';
                            break;
                        case 'xls':
                            $photo_url = '/web/img/xls.png';
                            break;
                        case 'lsx':
                            $photo_url = '/web/img/xls.png';
                            break;
                        case 'ppt':
                            $photo_url = '/web/img/ppt.png';
                            break;
                        case 'pptx':
                            $photo_url = '/web/img/ppt.png';
                            break;
                        case 'mp3':
                            $photo_url = '/web/img/music.png';
                            break;
                        case 'swf':
                            $photo_url = '/web/img/music.png';
                            break;
                        case 'mp4':
                            $photo_url = '/web/img/video.png';
                            break;
                        case 'avi':
                            $photo_url = '/web/img/video.png';
                            break;
                    }
                }
                $html .= '<div class="m-item"><img src="'.$photo_url.'" alt=""><button type="button" name="button" class="btn btn-default" onclick="return del_photo(this);" data-id="" data-pic="'.$value.'">删除</button></div>';

            }
            return jsonReturn($html);
        }

        //用户头像id插入数据库
        public function insert_head_img() {
            $photo_id = intval(input('post.id',0));
            $AdminUser = Db::name('AdminUser');
            $id = inval(input('session.adminInfo.id'));
            $update_id = $AdminUser->where(array('id'=>$id))->save(array('img'=>$photo_id));
            if($update_id){
                return jsonReturn(array('ok'=>1));
            }else{
                return jsonReturn(array('ok'=>0));
            }
        }
}
